<template>
  <div class="app-container goods-detail">
    <!-- <div class="filter-container"></div> -->
    <div class="goods-detail-title">拍品{{ formMode === 'add' ? '入库' : '编辑' }}</div>
    <goods-form
      ref="goodsForm"
      v-loading="detailLoading"
      :mode="formMode"
      :data="detail"
      @submit="handleSubmit"
      @goback="handleBack"
    />
  </div>
</template>
<script>
import GoodsForm from './components/GoodsForm'
import { getGoodsDetail, updateGoods } from '@/api/goods'
export default {
  components: { GoodsForm },
  data () {
    return {
      goodsId: '',
      detail: null,
      detailLoading: false,
      formMode: 'add' // add edit
    }
  },
  mounted () {
    const id = this.$route.params && this.$route.params.id
    if (id) {
      this.goodsId = id
      this.formMode = 'edit'
      this.fetchData()
    } else {
      this.formMode = 'add'
      this.detail = JSON.parse(JSON.stringify(this.$refs.goodsForm.form))
    }
  },
  methods: {
    fetchData () {
      this.detailLoading = true
      getGoodsDetail(this.goodsId).then(res => {
        this.detail = res.data
        this.detailLoading = false
      }).catch(err => {
        console.log(err)
        this.detailLoading = false
      })
    },
    handleSubmit (data) {
      updateGoods(data).then(res => {
        if (this.formMode === 'add') {
          this.$router.go(-1)
        }
        this.$refs.goodsForm.submitLoading = false
        this.$notify({
          message: res.msg,
          type: 'success',
          duration: 2000
        })
      }).catch(err => {
        this.$refs.goodsForm.submitLoading = false
        console.log(err)
      })
    },
    handleBack () {
      this.$router.go(-1)
    }
  }
}
</script>
<style lang="scss" scoped>
.goods-detail {
  width: 100%;
  max-width: 850px;
  margin: auto;
  &-title {
    line-height: 24px;
    font-size: 18px;
    color: #303133;
    text-align: center;
    padding: 20px 20px 35px;
  }
}
</style>
